package com.wwm.petadopt;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class LoginActivity extends Activity {
    private EditText editTextUsername;
    private EditText editTextPassword;
    private RequestQueue mRequestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar ab = getActionBar();
        if (ab != null)
        {
            View actionbarview=getLayoutInflater().inflate(R.layout.actionbar,null);
            ((TextView)actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.login);

            ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            ab.setCustomView(actionbarview);

        }
        editTextUsername = (EditText) findViewById(R.id.edittext_username);
        editTextPassword = (EditText) findViewById(R.id.edittext_password);
        Button buttonLogin = (Button) findViewById(R.id.button_login);
        buttonLogin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                final String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();
                Log.i("petadopt", "username: " + username + "; password: " + password);

                mRequestQueue = Volley.newRequestQueue(v.getContext());
                Map<String, String> map = new HashMap<String, String>();
                map.put("UserName", username);
                map.put("PassWord", password);

                JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, GlobalData.LOGIN_URL, new JSONObject(map), new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response)
                    {
                        Log.i("petadopt", response.toString());
                        //parseJSON(response);
                        try {
                            Log.i("petadopt", response.toString());
                            //	Log.i("petadopt", response.getString("success").toString());
                            int loginReqReturnValue = response.getInt("success");
                            Log.i("petadopt", Integer.toString(loginReqReturnValue));
                            if (loginReqReturnValue == 0)
                            {
                                Toast.makeText(LoginActivity.this, response.getString("message"), Toast.LENGTH_SHORT).show();
                                GlobalData.isLogin = true;
                                GlobalData.username = username;
                                LoginActivity.this.finish();
                            }
                            else
                            {
                                Toast.makeText(LoginActivity.this, response.getString("message"), Toast.LENGTH_SHORT).show();
                                return ;
                            }

                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        Log.i("petadopt", error.getMessage());
                    }
                });
                mRequestQueue.add(jr);
            }
        });
        Button buttonRegister=(Button)findViewById(R.id.button_fastregister);
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent("android.intent.action.REGISTER");
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
